$(function () {

    var insurance_form = $("#insurance_form");
    var carp_dialog = $("#carp_dialog");
    var carp_datagrid = $("#carp_datagrid");

    var client_list = $("#client_list");
    var client_dialog = $("#client_dialog");
    var car_list = $("#car_list");
    var car_dialog = $("#car_dialog");

    var methodObj = {
        save: function () {
            var rows = $("#product_table").datagrid("getRows");
            var ids = $.map(rows, function (item) {
                return item.id;
            })
            var amount = $("#totalMoney").text();
            //提交表单
            insurance_form.form("submit", {
                url: '/insuranceTS/saveOrUpdate.do',
                onSubmit: function (param) {
                    param.ids = ids;
                    param.amount = amount;
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', data.msg, 'info');
                        insurance_form.form("clear");
                        $("#product_table").datagrid("loadData", []);
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            });
        },
        carp_cancel: function () {
            carp_dialog.dialog("close");
        },
        //添加保险产品明细
        carp_add: function () {

            var row = carp_datagrid.datagrid("getSelected");

            $("#product_table").datagrid("appendRow", row);

            count();
            carp_dialog.dialog("close");

        },
        addProduct: function () {
            var textbox = $("#mech_combobox").combobox("getValue");
            var data_grid = null;
            $.get("/carproduct/getCarproductByMechId.do", {mechId: textbox}, function (data) {
                carp_datagrid.datagrid({
                    data: data,
                    fit: true,
                    fitColumns: true,
                    striped: true,
                    singleSelect: true,
                    onDblClickRow: function (index, row) {
                        methodObj.carp_add();
                    },
                    columns: [[
                        {field: 'id', title: 'id', width: 100, hidden: true},
                        {field: 'sn', title: '保险编号', width: 100},
                        {field: 'name', title: '保险名称', width: 100},
                        {
                            field: 'mech', title: '保险机构', width: 100, formatter: function (value, row, index) {

                                return row.mech != null ? row.mech.name : ""
                            }
                        },
                        {field: 'insured', title: '保额', width: 100},
                        {field: 'moneyYear', title: '基本年费', width: 100},
                        {
                            field: 'state', title: '销售状态', width: 100, formatter: function (value, row, index) {
                                return value ? "<font color='green'>在售</font>" : "<font color='red'>停售</font>"
                            }
                        },
                        {
                            field: 'deductState', title: '不计免赔', width: 100, formatter: function (value, row, index) {
                                return value ? "<font color='green'>是</font>" : "<font color='red'>否</font>"
                            }
                        },
                    ]],

                })

            })

            carp_dialog.dialog("setTitle", "添加产品");
            carp_dialog.dialog("open");
        },
        carfind: function () {
            car_dialog.dialog("open");
        },
        clientfind: function () {
            client_dialog.dialog("open");
        },

        carcommit: function () {
            //如果没有选择数据
            var row = car_list.datagrid("getSelected");
            if (!row) {
                //弹出提示框
                $.messager.alert("提示", "请选择数据", "warning");
                return;
            }

            //车辆信息回显
            row["carInformation.id"] = row.id;
            row["carInformation.name"] = row.name;
            row["carInformation.brand"] = row.brand;
            row["carInformation.type"] = row.type;
            row["carInformation.carNumber"] = row.carNumber;
            row["carInformation.airVolume"] = row.airVolume;
            row["carInformation.state"] = row.state;
            row["carInformation.inputTime"] = row.inputTime;
            row["carInformation.evaluation"] = row.evaluation;
            row["carInformation.carType"] = row.carType;

            insurance_form.form('load', row);

            car_dialog.dialog("close");
        },

        clientcommit: function () {
            //如果没有选择数据
            var row = client_list.datagrid("getSelected");
            if (!row) {
                //弹出提示框
                $.messager.alert("提示", "请选择数据", "warning");
                return;
            }

            //车辆信息回显
            row["client.id"] = row.id
            row["client.name"] = row.name;
            row["client.sex"] = row.sex;
            row["client.tel"] = row.tel;
            row["client.id_card"] = row.id_card;
            row["client.address"] = row.address;

            insurance_form.form('load', row);

            client_dialog.dialog("close");
        },
    }


    //事件统一绑定
    $("[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });


    car_list.datagrid({
        fit: true,
        rownumbers: true,
        fitColumns: true,
        pagination: true,
        singleSelect: true,
        url: '/carinformation/query.do',
        onDblClickRow: function (index, row) {
            methodObj.carcommit();
        },
        columns: [[
            {field: 'name', title: '车主姓名', width: 100},
            {field: 'inputTime', title: '购置日期', width: 100},
            {field: 'carNumber', title: '车牌号', width: 100},
            {field: 'brand', title: '品牌', width: 100},
            {field: 'type', title: '车型', width: 100}
        ]]
    }),

        client_list.datagrid({
            fit: true,
            rownumbers: true,
            fitColumns: true,
            pagination: true,
            singleSelect: true,
            onDblClickRow: function (index, row) {
                methodObj.clientcommit();
            },
            url: '/client/selectByStatus.do?status=0,1,2,3,4',
            columns: [[
                {field: 'name', title: '姓名', width: 100},
                {
                    field: 'sex', title: '性别', width: 100,
                    formatter: function (value, row, index) {
                        return value ? "男" : "女";
                    }
                },
                {field: 'tel', title: '联系方式', width: 100},
                {field: 'id_card', title: '身份证', width: 100},
                {field: 'address', title: '通讯地址', width: 100}
            ]]
        }),

        car_dialog.dialog({
            closed: true,
            width: 1000,
            height: 550,
            buttons: $("#carButtons"),
            title: '客户车辆信息'
        })

    client_dialog.dialog({
        closed: true,
        width: 1000,
        height: 550,
        buttons: $("#clientButtons"),
        title: '客户信息'
    })


    //品牌下拉框回显
    $("#carType").combobox({
        url: '/carinformation/cartype.do',
        panelHeight: true,
        valueField: 'id',
        textField: 'brand'
    })

    //承保机构回显
    $("#mech_combobox").combobox({
        url: "/mechanism/mech.do",
        panelHeight: true,
        valueField: 'id',
        textField: 'name'
    });


    carp_dialog.dialog({
        width: 700,
        height: 450,
        closed: true,
        buttons: '#carp_buttons',
    })

    $("#product_table").datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        singleSelect: true,
        toolbar: "#product_table_button",
        columns: [[
            {field: 'id', title: 'id', width: 100, hidden: true},
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'name', title: '保险名称', width: 100},
            {
                field: 'mech', title: '保险机构', width: 100, formatter: function (value, row, index) {

                    return row.mech != null ? row.mech.name : ""
                }
            },
            {field: 'insured', title: '保额', width: 100},
            {field: 'moneyYear', title: '基本年费', width: 100},
            {
                field: 'deductState', title: '不计免赔', width: 100, formatter: function (value, row, index) {
                    return value ? "<font color='green'>是</font>" : "<font color='red'>否</font>"
                }
            },
        ]],
    });

    $("#term").textbox({
        label: '保险时长',
        labelWidth: '70px',
        labelAlign: 'center',
        width: 140,
        onChange: function (newValue, oldValue) {
            var totalMoney = 0;
            var rows = $("#product_table").datagrid('getRows');
            console.log(rows);

            for (var i = 0; i <= rows.length - 1; i++) {
                var moneyYear = rows[i].moneyYear;
                console.log(typeof moneyYear);
                totalMoney = totalMoney + newValue * moneyYear;
            }
            console.log(totalMoney);
            $("#totalMoney").text(totalMoney);
        }
    })
});

//删除
function remove() {
    var row = $("#product_table").datagrid('getSelected');
    var index;
    if (!row) {
        $.messager.alert('温馨提示', '请选择一条数据！', 'info');
        return;
    }
    var index = $("#product_table").datagrid('getRowIndex', row);
    $("#product_table").datagrid('deleteRow', index);
    $("#product_table").datagrid('reload');

    count();
}

function count() {

    var value = $("#term").textbox("getValue");
    var totalMoney = 0;
    var rows = $("#product_table").datagrid('getRows');

    for (var i = 0; i <= rows.length - 1; i++) {
        var moneyYear = rows[i].moneyYear;
        totalMoney = totalMoney + value * moneyYear;
        // row["amount"] = row.sex;
    }
    $("#totalMoney").text(totalMoney);

}